# -*- coding:utf-8 -*-
import logging
import importlib

from common.cache import redis_cache

from django.conf import settings


_LOGGER = logging.getLogger(__name__)


class BaseTips(object):
    """
    小红点抽象类
    """

    def query(self, user_id):
        raise NotImplementedError('BaseTips is an Abstract Class')

    def update(self, user_id):
        raise NotImplementedError('BaseTips is an Abstract Class')

    def add(self, user_id):
        """
        增加一个tip
        """
        raise NotImplementedError('BaseTips is an Abstract Class')

    def remove(self, user_id):
        """
        移除一个tip
        """
        raise NotImplementedError('BaseTips is an Abstract Class')

    def set(self, user_id, count):
        """
        设置tips个数
        """
        raise NotImplementedError('BaseTips is an Abstract Class')

    def clear(self, user_id):
        """清空tips
        """
        raise NotImplementedError('BaseTips is an Abstract Class')


# 用于映射快捷方式的小红点key
CMD_TIPS_MAP = {
    '11#%s/user/newbie_task' % settings.WEB_APP_ROOT_URL: 'fresh_mission',
}


# 所有存在待完成事项提醒的并会存在于发现中的活动，都需要在下面配置, 对应的模块需要实现query_tips方法
CMD_MODULES = {
    # '11#%s/user/newbie_task' % settings.WEB_APP_ROOT_URL:
    #     importlib.import_module('common.mission.fresh_mission'),
}


# 所有可能出现在快捷入口的功能，都要在下面配置
CAMPAIGN_CONF = [
    # ('fresh_mission', '11#%s/user/newbie_task' % settings.WEB_APP_ROOT_URL,
    #     importlib.import_module('common.mission.fresh_mission')),
]


class CommandTips(BaseTips):
    """
    命令小红点，对应待完成事项
    """

    def query(self, user_id):
        command_dict = {}
        for item in CAMPAIGN_CONF:
            # check new tips
            tip_name, cmd, relation_module = item
            add_count = self.query_by_cmd(user_id, cmd)
            if add_count > 0:
                command_dict.update({tip_name: add_count})
        return command_dict

    def query_by_cmd(self, user_id, cmd, auto_ack=False):
        count = 0
        relation_module = CMD_MODULES.get(cmd)
        if relation_module:
            query_tips = getattr(relation_module, 'query_tips')
            if query_tips:
                count = query_tips(user_id)
        return count


def query_all(user_id, cvc, chn):
    tips = redis_cache.get_user_tips(user_id)
    if not tips:
        tips = {}
        command_tips = CommandTips().query(user_id)
        tips.update(command_tips)
        redis_cache.set_user_tips(user_id, tips)
    return tips
